package com.zhangmeng.online.exam.config;

import com.zhangmeng.jwt.config.SecurityAuthenticationSuccessHandler;
import com.zhangmeng.jwt.dto.LoginUser;
import com.zhangmeng.jwt.dto.Token;
import com.zhangmeng.jwt.response.Result;
import com.zhangmeng.jwt.response.StatusCode;
import com.zhangmeng.jwt.service.TokenService;
import com.zhangmeng.jwt.util.ResponseUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zm
 * @date 2025/3/22 15:36
 * @version: 1.0
 */
@Component
public class LoginSysLog extends SecurityAuthenticationSuccessHandler {

    @Autowired
    private TokenService tokenService;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Token token = tokenService.saveToken(loginUser);
        String message = "登录成功";
        ResponseUtil.responseJson(response, HttpStatus.OK.value(), new Result(true, StatusCode.OK,message, token));
    }
}
